package com.iqiyi.pps.epg.core.model.stream;

import knowledge.pub.Common;
import knowledge.pub.Properties;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "live_channel_stream")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class LiveChannelStream {
    public static final int STATUS_ACTIVED = 1;
    public static final int STATUS_IDLED = 0;
    public static final int STATUS_DELETE = -1;
    public static final int PEERCOUNT_MIN = 0;
    public static final int PEERCOUNT_MAX = 157;
    public static final int BCSNUM_MIN = 3;
    public static final int BCSNUM_MAX = 10;

    public static final int ENCODE_H264 = 0;
    public static final int ENCODE_H265 = 1;

    public static final int PLAY_TYPE_CUT = 0;
    public static final int PLAY_TYPE_TIMING = 1;

    private Long id;
    private Long fatherId;
    private String streamName;
    private String streamIp = "";
    private String encodedIp = "";
    private String inputBits = "";
    private String outputBits = "";
    private String streamType = "";
    private Long bitRate = 0L;
    private String aspectRatio = "";
    private String screenSize = "";
    private String frameRate = "";
    private String video = "";
    private String audio = "";
    private Integer peerCount = 0;
    private int bcsNum = 0;
    private int cutterIdFlv = 0;
    private int cutterIdTs = 0;
    private Boolean isEnable = true;
    private Integer status = 0;
    private Integer rtmpSync;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;
    private String dest;
    private int streamApiVersion = 0;
    private String dolbyStreamName = "";
    private String dolbyStreamUrl = "";
    private String dolbyAudioChannel = "";

    private int encodeType = 0;
    private String streamAddress = "";
    private int playType;

    private int sliceIndex = -1;//切片索引

    private String streamUrls = "";
    private String dolbyStreamUrls = "";

    private int backupEachOther = 1;

    public static final Map<String, String> LIVE_CHANNEL_STREAM_STATUS_MAP = new HashMap<String, String>() {{
        put("0", "未编码");
        put("1", "在编码");
        put("-1", "删除");
    }};

    public static final Map<Integer, String> STREAM_API_VERSION_MAP = new HashMap<Integer, String>() {{
        put(0, "1.2");
        put(1, "2.0");
    }};

    public static final Map<Integer, String> TS_STREAM_API_VERSION_MAP = new HashMap<Integer, String>() {{
        put(0, "1.0");
        put(1, "2.0");
    }};

    public static final Map<Integer, String> DOLBY_STREAM_API_VERSION_MAP = new HashMap<Integer, String>() {{
        put(0, "1.0");
        put(1, "2.0");
    }};

    public static final Map<String, String> STREAM_TYPE_RESOLUTION_MAP = new HashMap<String, String>() {{
        put("TOP_SPEED", "0");
        put("SMOOTH", "1");
        put("HIGH_DEFINITION", "2");
        put("SUPER_CLEAR", "3");
        put("RESOLUTION_720P", "3");
        put("RESOLUTION_1080P", "4");
        put("SPECIAL_H5", "0");
        put("RESOLUTION_2K", "5");
        put("RESOLUTION_4K", "6");
    }};

    public Common.StreamType toQipuStreamType() {
        if ("TOP_SPEED".equals(this.streamType)) {
            return Common.StreamType.TOP_SPEED;
        } else if ("SMOOTH".equals(this.streamType)) {
            return Common.StreamType.SMOOTH;
        } else if ("HIGH_DEFINITION".equals(this.streamType)) {
            return Common.StreamType.HIGH_DEFINITION;
        } else if ("SUPER_CLEAR".equals(this.streamType)) {
            return Common.StreamType.SUPER_CLEAR;
        } else if ("RESOLUTION_720P".equals(this.streamType)) {
            return Common.StreamType.RESOLUTION_720P;
        } else if ("RESOLUTION_1080P".equals(this.streamType)) {
            return Common.StreamType.RESOLUTION_1080P;
        } else if ("SPECIAL_H5".equals(this.streamType)) {
            return Common.StreamType.SPECIAL_H5;
        } else if ("RESOLUTION_2K".equals(this.streamType)) {
            return Common.StreamType.RESOLUTION_2K;
        } else if ("RESOLUTION_4K".equals(this.streamType)) {
            return Common.StreamType.RESOLUTION_4K;
        } else {
            return null;
        }
    }

    public Properties.StreamList.StreamInfo.StreamType toTvQipuStreamType() {
        if ("TOP_SPEED".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.TOP_SPEED;
        } else if ("SMOOTH".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.SMOOTH;
        } else if ("HIGH_DEFINITION".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.HIGH_DEFINITION;
        } else if ("SUPER_CLEAR".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.SUPER_CLEAR;
        } else if ("RESOLUTION_720P".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.RESOLUTION_720P;
        } else if ("RESOLUTION_1080P".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.RESOLUTION_1080P;
        } else if ("SPECIAL_H5".equals(this.streamType)) {
            return Properties.StreamList.StreamInfo.StreamType.SPECIAL_H5;
        } else {
            return null;
        }
    }

    public String fetchRtmpUrlUtil() {
        if(StringUtils.isBlank(this.streamIp) || StringUtils.isBlank(this.streamName)) {
            return "";
        }
        return String.format("rtmp://%s/live/%s", this.streamIp, this.streamName);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "father_id")
    public Long getFatherId() {
        return fatherId;
    }

    public void setFatherId(Long fatherId) {
        this.fatherId = fatherId;
    }

    @Column(name = "stream_name")
    public String getStreamName() {
        return streamName;
    }

    public void setStreamName(String streamName) {
        this.streamName = streamName;
    }

    @Column(name = "stream_ip", length = 15)
    public String getStreamIp() {
        return streamIp;
    }

    public void setStreamIp(String streamIp) {
        this.streamIp = streamIp;
    }

    @Column(name = "encoded_ip", length = 15)
    public String getEncodedIp() {
        return encodedIp;
    }

    public void setEncodedIp(String encodedIp) {
        this.encodedIp = encodedIp;
    }

    @Column(name = "input_bits")
    public String getInputBits() {
        return inputBits;
    }

    public void setInputBits(String inputBits) {
        this.inputBits = inputBits;
    }

    @Column(name = "output_bits")
    public String getOutputBits() {
        return outputBits;
    }

    public void setOutputBits(String outputBits) {
        this.outputBits = outputBits;
    }

    @Column(name = "stream_type")
    public String getStreamType() {
        return streamType;
    }

    public void setStreamType(String streamType) {
        this.streamType = streamType;
    }

    @Column(name = "bit_rate")
    public Long getBitRate() {
        return bitRate;
    }

    public void setBitRate(Long bitRate) {
        this.bitRate = bitRate;
    }

    @Column(name = "aspect_ratio")
    public String getAspectRatio() {
        return aspectRatio;
    }

    public void setAspectRatio(String aspectRatio) {
        this.aspectRatio = aspectRatio;
    }

    @Column(name = "screen_size")
    public String getScreenSize() {
        return screenSize;
    }

    public void setScreenSize(String screenSize) {
        this.screenSize = screenSize;
    }

    @Column(name = "frame_rate")
    public String getFrameRate() {
        return frameRate;
    }

    public void setFrameRate(String frameRate) {
        this.frameRate = frameRate;
    }

    @Column(name = "video")
    public String getVideo() {
        return video;
    }

    public void setVideo(String video) {
        this.video = video;
    }

    @Column(name = "audio")
    public String getAudio() {
        return audio;
    }

    public void setAudio(String audio) {
        this.audio = audio;
    }
    @Column(name = "peer_count")
    public Integer getPeerCount() {
        return peerCount;
    }

    public void setPeerCount(Integer peerCount) {
        this.peerCount = peerCount;
    }

    @Column(name = "bcs_num")
    public int getBcsNum() {
        return bcsNum;
    }

    public void setBcsNum(int bcsNum) {
        this.bcsNum = bcsNum;
    }

    @Column(name = "cutter_id_ts")
    public int getCutterIdTs() {
        return cutterIdTs;
    }

    public void setCutterIdTs(int cutterIdTs) {
        this.cutterIdTs = cutterIdTs;
    }

    @Column(name = "cutter_id_flv")
    public int getCutterIdFlv() {
        return cutterIdFlv;
    }

    public void setCutterIdFlv(int cutterIdFlv) {
        this.cutterIdFlv = cutterIdFlv;
    }

    @Column(name = "is_enable", updatable = false)
    public Boolean getIsEnable() {
        return isEnable;
    }

    public void setIsEnable(Boolean isEnable) {
        this.isEnable = isEnable;
    }
    @Column(name = "status")
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Column(name = "rtmp_sync")
    public Integer getRtmpSync() {
        return rtmpSync;
    }

    public void setRtmpSync(Integer rtmpSync) {
        this.rtmpSync = rtmpSync;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time", insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    @Column(name = "dest", insertable = true, updatable = false, length = 15, columnDefinition = "")
    public String getDest() {
        return dest;
    }

    public void setDest(String dest) {
        this.dest = dest;
    }

    @Column(name = "stream_api_version")
    public int getStreamApiVersion() {
        return streamApiVersion;
    }

    public void setStreamApiVersion(int streamApiVersion) {
        this.streamApiVersion = streamApiVersion;
    }

    @Column(name = "dolby_stream_name")
    public String getDolbyStreamName() {
        return dolbyStreamName;
    }

    public void setDolbyStreamName(String dolbyStreamName) {
        this.dolbyStreamName = dolbyStreamName;
    }

    @Column(name = "dolby_stream_url")
    public String getDolbyStreamUrl() {
        return dolbyStreamUrl;
    }

    public void setDolbyStreamUrl(String dolbyStreamUrl) {
        this.dolbyStreamUrl = dolbyStreamUrl;
    }

    @Column(name = "dolby_audio_channel")
    public String getDolbyAudioChannel() {
        return dolbyAudioChannel;
    }

    public void setDolbyAudioChannel(String dolbyAudioChannel) {
        this.dolbyAudioChannel = dolbyAudioChannel;
    }

    @Column(name = "encode_type")
    public int getEncodeType() {
        return encodeType;
    }

    public void setEncodeType(int encodeType) {
        this.encodeType = encodeType;
    }

    @Column(name = "stream_address")
    public String getStreamAddress() {
        return streamAddress;
    }

    public void setStreamAddress(String streamAddress) {
        this.streamAddress = streamAddress;
    }

    @Column(name = "stream_urls", updatable = false)
    public String getStreamUrls() {
        return streamUrls;
    }

    public void setStreamUrls(String streamUrls) {
        this.streamUrls = streamUrls;
    }

    @Column(name = "dolby_stream_urls", updatable = false)
    public String getDolbyStreamUrls() {
        return dolbyStreamUrls;
    }

    public void setDolbyStreamUrls(String dolbyStreamUrls) {
        this.dolbyStreamUrls = dolbyStreamUrls;
    }

    @Column(name = "play_type")
    public int getPlayType() {
        return playType;
    }

    public void setPlayType(int playType) {
        this.playType = playType;
    }

    @Column(name ="backup_each_other")
    public int getBackupEachOther() {
        return backupEachOther;
    }

    public void setBackupEachOther(int backupEachOther) {
        this.backupEachOther = backupEachOther;
    }

    @Column(name = "slice_index")
    public int getSliceIndex() {
        return sliceIndex;
    }

    public void setSliceIndex(int sliceIndex) {
        this.sliceIndex = sliceIndex;
    }
}
